package com.laiyima.zhongjiang.linghuilv.demo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.bean.TaxListBean;
import com.laiyima.zhongjiang.linghuilv.demo.home.TaxDetailsActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TaxListbeanAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<TaxListBean> mBean;
    private String TAG="TaxListbeanAdapter";

    public TaxListbeanAdapter(Context context, List<TaxListBean> maxbeanDatas){
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mBean = maxbeanDatas;
    }
    public void updateListView(List<TaxListBean> list){
        this.mBean = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mBean.size();
    }

    @Override
    public Object getItem(int position) {
        return mBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int  position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(viewHolder==null){
            convertView = mInflater.inflate(R.layout.mytaxlist,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.mId = convertView.findViewById(R.id.list_tax_id);
            viewHolder.mUid = convertView.findViewById(R.id.list_tax_uid);
            viewHolder.mTaxAmount = convertView.findViewById(R.id.list_moeny);
            viewHolder.mCtime = convertView.findViewById(R.id.list_time);
            viewHolder.mCheck_res = convertView.findViewById(R.id.list_check_res);
            viewHolder.mlist_details = convertView.findViewById(R.id.list_details);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TaxListBean mTaxbean = mBean.get(position);
        viewHolder.mId.setText(mTaxbean.getId());
        viewHolder.mUid.setText(mTaxbean.getUid());
        viewHolder.mTaxAmount.setText(mTaxbean.getTaxAmount()+"元");


        viewHolder.mCtime.setText(stampToDate(mTaxbean.getCtime()));




        viewHolder.mCheck_res.setText(mTaxbean.getCheck_res());
        if(mTaxbean.getCheck_res().equals("通过")){
            viewHolder.mCheck_res.setTextColor(Color.argb(100,18,108,228));
        }else if(mTaxbean.getCheck_res().equals("未通过")){
            viewHolder.mCheck_res.setTextColor(Color.RED);
        }

        viewHolder.mlist_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddTax(position);
            }
        });
        return convertView;
    }
    private void showAddTax(int position) {
        String mId=mBean.get(position).getId();
        Intent intent = new Intent(mContext, TaxDetailsActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        intent.putExtra("id", mId);
        mContext.startActivity(intent);
    }
    public static class ViewHolder{
        TextView mId;
        TextView mUid;
        TextView mTaxAmount;
        TextView mCtime;
        TextView mCheck_res;
        TextView mlist_details;
    }
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(Long.parseLong(s+"000"));
        res = simpleDateFormat.format(date);
        return res;
    }


}
